package com.iemes.controller.transport;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.ClientFormMap;
import com.iemes.entity.MotorcadeInfoFormMap;
import com.iemes.entity.TransporTrecordFormMap;
import com.iemes.entity.TransportDriverinfoFormMap;
import com.iemes.entity.TransportTransporinfoFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.client.ClientMapper;
import com.iemes.mapper.motorcade.MotorcadeMapper;
import com.iemes.mapper.transport.TransportDriverinfoMapper;
import com.iemes.mapper.transport.TransportTransporinfoMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/ransportTransporinfo/")
@Controller
public class TransportTransporinfoController extends BaseController {
	@Inject
	private TransportTransporinfoMapper transportTransporinfoMapper;
	@Inject
	private MotorcadeMapper motorcadeMapper;
	@Inject
	private ClientMapper clientMapper;
	@Inject
	private TransportDriverinfoMapper transportDriverinfoMapper;

	// 车辆调度明细
	@RequestMapping("/examineransportTransporinfo")
	public String examineTransport(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/transport/transporinfo/transpor_list";
	}

	@ResponseBody
	@RequestMapping("/transportTransporinfo_findbyPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception {
		TransportTransporinfoFormMap transportTransporinfoFormMap = getFormMap(TransportTransporinfoFormMap.class);
		List<Map<String, Object>> transportTransporinfoFormMaps = transportTransporinfoMapper
				.getAllInfo(transportTransporinfoFormMap);
		for (Map<String, Object> map : transportTransporinfoFormMaps) {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date d1 = df.parse(map.get("plangoDate").toString());
			Date d2 = df.parse(map.get("practicalgoDate").toString());
			Date d3 = df.parse(map.get("practicalgoDate").toString());
			Date d4 = df.parse(map.get("carDispatchDate").toString());
			long timelag = d1.getTime() - d2.getTime();// 这样得到的差值是秒级别
			long toadforDate = d3.getTime() - d4.getTime();// 这样得到的差值是秒级别
			timelag = timelag / 1000 / 60;
			toadforDate = toadforDate / 1000 / 60;
			if (timelag >= 0) {
				map.put("timelag", timelag);
			} else if (timelag <= 0) {
				timelag = 0;
				map.put("timelag", timelag);
			}
			if (toadforDate >= 0) {
				map.put("toadforDate", toadforDate);
			} else if (timelag <= 0) {
				toadforDate = 0;
				map.put("toadforDate", toadforDate);
			}
			// map.put("minutes", minutes);
		}
		LuogicPaging(pageNow, pageSize, transportTransporinfoFormMaps);
		return pageView;
	}

	// 修改车队调度明细
	@RequestMapping("/transportTransporinfo_edit")
	public String tranup(Model model) {
		String id = getPara("id");
		List<Map<String, Object>> transportTransporinfoFormMaps = transportTransporinfoMapper.getAllInfo(null);

		// 获取对象
		if (Common.isNotEmpty(id)) {
			TransportTransporinfoFormMap transportTransporinfoFormMap = baseMapper.findbyFrist("id", id,
					TransportTransporinfoFormMap.class);
			for (Map<String, Object> map : transportTransporinfoFormMaps) {
				if (map.get("id") == transportTransporinfoFormMap.get("id")) {
					transportTransporinfoFormMap.put("licenseNo", map.get("licenseNo"));
					transportTransporinfoFormMap.put("carBossName", map.get("carBossName"));
				}
			}
			model.addAttribute("tranedit", transportTransporinfoFormMap);
		}
		return Common.BACKGROUND_PATH + "/transport/transporinfo/transport_edit";
	}

	@ResponseBody
	@RequestMapping(value = "/transportTransporinfo_editEntity")
	@SystemLog(module = "车队调度明细", methods = "调度明细-编辑调度明细") // 凡需要处理业务逻辑的.都需要记录操作日志
	@Transactional
	public String editEntity() {
		TransportTransporinfoFormMap transportTransporinfoFormMap = getFormMap(TransportTransporinfoFormMap.class);
		try {
			transportTransporinfoMapper.editEntity(transportTransporinfoFormMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "fasle";
		}
		return "success";
	}

	// 添加车队调度明细
	@RequestMapping("/transportTransporinfo_add")
	public String addUI(Model model) {
		List<MotorcadeInfoFormMap> motorcadeInfoFormMap = motorcadeMapper.findByWhere(new MotorcadeInfoFormMap());
		List<ClientFormMap> clientFormMap = clientMapper.findByWhere(new ClientFormMap());
		model.addAttribute("motorcadeInfoFormMap", motorcadeInfoFormMap);
		model.addAttribute("clientFormMap", clientFormMap);
		return Common.BACKGROUND_PATH + "/transport/transporinfo/transport_add";
	}
	
	//根据车牌id显示对应的司机
	@RequestMapping("/transportTransporinfo_id")
	@ResponseBody
	public Object tranId() {
		String licenseNo = getPara("id").toString();
		if(Integer.valueOf(licenseNo)!=0){
		TransportTransporinfoFormMap transportTransporinfoFormMap=transportTransporinfoMapper.getAlllicenseNo(licenseNo);
		String id=transportTransporinfoFormMap.get("id").toString();
		List<Map<String, Object>> transportTransporinfoFormMaps=transportTransporinfoMapper.getAllcarBossId(id);
		return transportTransporinfoFormMaps;
		}else{
			return "false";
		}
	}

	@ResponseBody
	@RequestMapping("/transport_addEntity")
	@SystemLog(module = "车队调度明细", methods = "调度明细-新增车队调度明细")
	public String addEntity() {
		TransportTransporinfoFormMap transportTransporinfoFormMap = getFormMap(TransportTransporinfoFormMap.class);
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		transportTransporinfoFormMap.put("byuser_id", userFormMap.getInt("id"));
		transportTransporinfoFormMap.set("status", "1");
		transportTransporinfoFormMap.set("warehouseKeeperId", "11");
		try {
			transportTransporinfoMapper.addEntity(transportTransporinfoFormMap);
		} catch (Exception e) {
			return "false";
		}
		return "success";
	}

	// 删除调度明细
	@ResponseBody
	@RequestMapping("transportTransporinfo_deleteEntity")
	@Transactional(readOnly = false) // 需要事务操作必须加入此注解
	@SystemLog(module = "删除调度明细", methods = "pod按钮-删除pod按钮") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		TransportTransporinfoFormMap transportTransporinfoFormMap = getFormMap(TransportTransporinfoFormMap.class);
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		for (int i = 0; i < str.length; i++) {
			transportTransporinfoFormMap.set("status", 2);
			transportTransporinfoFormMap.put("id", str[i]);
			transportTransporinfoMapper.editEntity(transportTransporinfoFormMap);
		}

		return "success";
	}
}
